<template>
    <div class="dahezi">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>我的文章</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button v-if="shiyisheng=='医生'?true:false"  @click="dianji" type="success" >发表文章</el-button>
        <div class="hezi">
            <div class="hezi2">
                <div class="hezi3">
                    <div class="bbbbb" v-infinite-scroll="load">

                        <!--                  内容-->

                            <div class="jinrirediang1" v-for="(p,index) in sousuowenzhang" :key="index">
                                <!--            帖子标题-->

                                <!--                一条帖子的框框，从这里开始v-for遍历-->

                                <div class="rediang1" >
                                    <router-link :to="{name:'wenzhangxiangqing',
                                 query:{id:p.id,
                                        title:p.title,
                                        content:p.content,
                                        time:p.time
                                 }}">


                                        <div class="zuixintiezi2" >
                                            {{p.title}}
                                        </div>
                                        <div class="contentcss">
                                            <div v-html="p.content.length > 100 ? p.content.slice(0, 100) + '...' :p.content" />
                                        </div>

                                </router-link>
                                    <div class="xianshicount">
                                        👍{{p.countGetLike}}  ☆{{p.countCollect}}   👀{{p.countLook}}
                                    </div>
                                    <div class="xianshicount">
                                        <el-link type="danger" size="middle" @click="handleDelete(p.id)" icon="el-icon-delete">删除</el-link>
                                    </div>
                                </div>

                                </div>
                    </div>
                </div>
            </div>
        </div>
        <!--        添加文章对话框-->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="文章作者" prop="userName">
                    <el-tag type="info" effect="plain">{{form.userName}}</el-tag>
                    <!--                        <el-input v-model="form.userName" placeholder="请输入文章笔名" />-->
                </el-form-item>
                <el-form-item label="文章标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入文章标题" />
                </el-form-item>
                <el-form-item label="文章内容">
                    <editor v-model="form.content" :min-height="192"/>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="tijiao">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import marked from "marked";
    import hljs from "highlight.js";
    import "highlight.js/styles/monokai-sublime.css";
    import Editor from '@/components/Editor';

    export default {
        name: "wodewenzhang",
        data(){
            return{
                sousuowenzhang:[],
                shiyisheng:'',
                form: {
                    title:'',
                    content:'',
                    time:'',
                    userName:'',
                    userId:''
                },
                // 表单校验
                rules: {},
                // 修改对话框开关控制
                open :false,
                title:'发布文章'
            }
        },
        components: { Editor },
        created() {
            // this.getwenzhang()
            this.getuserlll()
        },
        mounted() {
            //初始化
            this.getList();
            marked.setOptions({
                renderer: new marked.Renderer(),
                highlight: function (code) {
                    return hljs.highlightAuto(code).value;
                },
                pedantic: false,
                gfm: true,
                tables: true,
                breaks: true,
                sanitize: false,
                smartLists: true,
                smartypants: false,
                xhtml: true,
                count: 0,
            });
        },

        methods:{
            async getuserlll(){
                const {data:jieguo}=await this.$http.get('user/info',{headers:{
                        'Authorization':window.sessionStorage.getItem('token')
                    }})

                this.shiyisheng=jieguo.data.userRole
                this.form.userName=jieguo.data.basicUserInformation.username
                this.form.userId=jieguo.data.basicUserInformation.id
                this.basicUserInformation=jieguo.data.basicUserInformation
                this.sousuowenzhang=jieguo.data.articles
                console.log(this.sousuowenzhang)
            },
            //一上来请求数据
            // async   getwenzhang(){
            //
            //     //请求文章
            //     const res= await this.$http.get('article/userList',{headers:{
            //             'Authorization':window.sessionStorage.getItem('token')
            //         }})
            //     console.log('??',res.data.data.data)
            //     this.sousuowenzhang=res.data.data.data
            // },
            //点击打开发布文章对话框
            dianji(){
                this.open=true
            },
            /** 提交按钮 */
            tijiao() {
                if (!this.form.title) {
                    this.$message('标题不能为空');
                    return false
                }
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.post('article/insertArticle', this.form).then(response => {
                    console.log(response)
                    if (response.data.code === 200) {
                        this.$message({showClose: true, message: "修改成功", type: "success"})
                        this.open = false;
                        this.getuserlll()
                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form.title=''
                this.form.content=''
                this.form.time=''
                this.form.userName=''
                this.form.userId=''


            },
            load(){
                this.count += 2;
            },
            /** 删除按钮操作 */
            handleDelete(id) {
                this.$confirm('是否确认删除', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(async () => {
                    this.$http.delete(`article/`+id).then(response => {
                        if (response.data.code == 200) {
                            this.$message({
                                showClose: true,
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getuserlll();
                        } else {
                            this.$message({
                                showClose: true,
                                message: response.data.msg,
                                type: response.data.code
                            });
                        }
                    });
                }).catch(() => {

                });
            },
        }
    }
</script>

<style scoped>
    .hezi{
        width: 80%;
        min-height: 900px;
        height: auto!important;
        display: flex;
        justify-content:center;
        align-items:center;
    }
    .hezi2{
        width: 100%;
        min-height: 850px;
        height: auto!important;
        margin-top: 3px;
        /*display: flex;*/
        background-color: white;
    }
    .hezi3{
        width: 100%;
    }

    .dahezi{
        padding-top: 3px;
        background-color: #fffacd;
        min-height: 1000px;
        height: auto!important;
    }
    .bbbbb{
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        overflow: auto;
        min-height: 850px;
        width: 100%;
    }
    .jinrirediang1{
        height: 200px;
        width: 100%;
        margin-bottom: 10px;
    }
    /*最新帖子标题的样式*/
    .zuixintiezi2{
        height: 20%;
        margin-left: 20px;
        text-align: left;
        color: black;
        font-size: 18px;
        color: #111111;
    }
    .rediang1{
        border: #FFFACD solid 2px;
        background: #FFFFE0;
        margin-top: 10px;
        height: 80%;
        margin-left: 15%;
        width: 70%;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #111111;
    }
    .contentcss{
        height: 50%;
        color: #111111;
    }
    .xianshicount{
        height: 15%;
        color: #111111;
        float: right;
    }
    .el-button{
        margin-left: 200px;
    }
</style>
